const router = require('express').Router({

});
const { check, validationResult } = require('express-validator'); //数据验证
const teacherControllers = require("../../controllers/teacherControllers");
/* GET */
router.get(
  '/',
  [
    check('pageNo').notEmpty().withMessage('页码不能为空'),
    check('pageSize').notEmpty().withMessage('条数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }
    let data = await teacherControllers
                    .selectList(
                      req.query.pageNo,
                      req.query.pageSize,
                      req.query.tname,
                      req.query.department
                    );

    res.sendResult(data,200,"请求成功");
    
  }
);


router.post(
  '/',
  [
    check('teacher_department_id').notEmpty().withMessage('teacher_department_id参数不能为空'),
    check('teacher_number').notEmpty().withMessage('teacher_number参数不能为空')
                            .custom(student_number => {
                              if ( !(/1\d{10}/.test( student_number )) ) {
                                throw new Error('teacher_number不是合法手机号')
                              }
                              return true
                            }),
    check('teacher_name').notEmpty().withMessage('teacher_name参数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data1 = await teacherControllers.selectByNameAndCondition(req.body.teacher_number, null, "0");

    if (data1) {
      res.sendResult(null, 400, "添加失败,员工工号(手机号)已存在");
      return;
    }
    let data2 = await teacherControllers.selectByNameAndCondition(req.body.teacher_number, null, "1");

    if (data2) {
      await teacherControllers.updateByDid(req.body.teacher_department_id, req.body.teacher_number,req.body.teacher_name,data2.teacher_id, 0)
    } else {
      await teacherControllers.insert(req.body)
    }
    res.sendResult({}, 201, "添加成功,默认密码是 123456");
  }
);

router.put(
  '/',
  [
    check('teacher_id').notEmpty().withMessage('teacher_id参数不能为空'),
    check('teacher_department_id').notEmpty().withMessage('teacher_department_id参数不能为空'),
    check('teacher_number').notEmpty().withMessage('teacher_number参数不能为空')
                            .custom(student_number => {
                              if ( !(/1\d{10}/.test( student_number )) ) {
                                throw new Error('teacher_number不是合法手机号')
                              }
                              return true
                            }),
    check('teacher_name').notEmpty().withMessage('teacher_name参数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data1 = await teacherControllers.selectById(req.body.teacher_id);
    if (!data1) {
      res.sendResult(null, 400, "更新失败,teacher_id不存在");
      return;
    }

    let data2 = await teacherControllers.selectByNameAndCondition(req.body.teacher_number, req.body.teacher_id);
    if (data2) {
      res.sendResult(null, 400, "更新失败,员工工号(手机号)已存在");
      return;
    }

    let flag = await teacherControllers.updateByDid(req.body.teacher_department_id,
                                                    req.body.teacher_number,
                                                    req.body.teacher_name,
                                                    req.body.teacher_id);
    res.sendResult({}, 202, flag ? "更新成功" : "数据已更新");
  }
);
  
router.delete(
  '/',
  [
    check('teacher_id').notEmpty().withMessage('teacher_id参数不能为空'),
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data = await teacherControllers.selectById(req.query.teacher_id);
    if (!data) {
      res.sendResult(null, 400, "删除失败,teacher_id不存在");
      return;
    }

    let flag = await teacherControllers.deleteByDid(req.query.teacher_id);
    res.sendResult({}, 203, flag ? "删除成功" : "数据已删除");
  }
);

module.exports = router;
